/***************************************************************************
 * amlicence.c:  Araxis Merge licence checker.
 *
 * Copyright (C) 2010 Araxis Ltd.
 *
 * This code released by Araxis Ltd under GPL v3.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ***************************************************************************/

/**************************************************************************
 * Includes
 **************************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "amlicence.h"

/**************************************************************************
 * Manfest Constants
 **************************************************************************/

/**************************************************************************
 * Macros
 **************************************************************************/

/**************************************************************************
 * Types
 **************************************************************************/

/**************************************************************************
 * Local Variables
 **************************************************************************/

/**************************************************************************
 * Local Functions
 **************************************************************************/

static unsigned char base64CharToByte(unsigned char ch)
{
    static const char *base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    const char *p = strchr(base64, ch);
    return p ? (p - base64) : 0;
}


/*
 * Warning: assumes base64 contains enough to supply byteCount worth of decoded bytes.
 */
static void decodeBase64(unsigned char *bytes, size_t byteCount, const char *base64)
{
    unsigned char *b = bytes;

    while ((b + 3) <= (bytes + byteCount))
    {
        unsigned char b1 = base64CharToByte(*base64++);
        unsigned char b2 = base64CharToByte(*base64++);
        unsigned char b3 = base64CharToByte(*base64++);
        unsigned char b4 = base64CharToByte(*base64++);
        *b++ = (b1 << 2) | (b2 >> 4);
        *b++ = (b2 << 4) | (b3 >> 2);
        *b++ = (b3 << 6) | b4;
    }
}


static unsigned short swapLEShortToHost(unsigned short n)
{
/* __BIG_ENDIAN__ is defined by GCC for big-endian targets. */
#if defined(__BIG_ENDIAN__)
  return (n << 8) | (n >> 8);
#else
  return n;
#endif
}


/**************************************************************************
 * Global Functions
 **************************************************************************/

/** Check the licence type for the Araxis Merge installation.
 *
 * \param[in] serialNumber  The 53 character long product key.
 */
amlicence_t AmLicenceCheck(const char *serialNumber)
{
    if(!serialNumber)
    {
        return AMLICENCE_NOT_FOUND;
    }

    size_t serialNumberLength = strlen(serialNumber);

    if(serialNumberLength != 53)
    {
        return AMLICENCE_NOT_FOUND;
    }

    /* Reverse conversion of easily misread characters and strip separators */
    char *convertedSerialNumber = (char*)malloc(serialNumberLength + 1);

    for (size_t m = 0, n = 0; n <= serialNumberLength; n++)
    {
        switch (serialNumber[n])
        {
            case '[': convertedSerialNumber[m++] = '1'; break;
            case ']': convertedSerialNumber[m++] = 'i'; break;
            case '*': convertedSerialNumber[m++] = 'l'; break;
            case '#': convertedSerialNumber[m++] = '0'; break;
            case '@': convertedSerialNumber[m++] = 'O'; break;
            case ':': break;
            default: convertedSerialNumber[m++] = serialNumber[n]; break;
        }
    }

    /* Convert the base64 string into bytes */
    unsigned char bytes[36];
    decodeBase64(bytes, 36, convertedSerialNumber);

    free(convertedSerialNumber);

    /* Inspect contents of information field (w/o sn validation) */
    unsigned short *packed1 = (unsigned short*)bytes;
    unsigned short  productType = (swapLEShortToHost(*packed1) >> 13) & 7;

    switch (productType)
    {
        case 2:  return AMLICENCE_PRO;
        case 3:  return AMLICENCE_STD;
        case 4:  return AMLICENCE_PRO_EVAL;
        default: return AMLICENCE_NOT_FOUND;
    }
}

/* END OF FILE */





